//
//  ViewController.m
//  coreanimation
//
//  Created by Pro on 2022/1/10.
//

#import "ViewController.h"

#import "CustomView.h"

@interface ViewController ()<CALayerDelegate>
@property(nonatomic,strong)UIView * outView;
@property(nonatomic,strong)UIView * coneView;
@property(nonatomic,strong)UIView * shipView;
@property(nonatomic,strong)UIView * iglooView;
@property(nonatomic,strong)UIView * anchorView;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)CALayer * hourLayer;
@property(nonatomic,strong)CALayer * minuteLayer;
@property(nonatomic,strong)CALayer * secondLayer;
@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,strong)UIImageView * hourImageView;
@property(nonatomic,strong)UIImageView * minuteImageView;
@property(nonatomic,strong)UIImageView * secondImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self chapter3];
//    [self chapter4];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    //draw a thick red circle
    CGContextSetLineWidth(ctx, 10.0f);
    CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);
    CGContextStrokeEllipseInRect(ctx, layer.bounds);
}

-(void)chapter4 {
    self.outView = [[UIView alloc] initWithFrame:CGRectMake(57, 100, 300, 300)];
    self.outView.layer.contents = (__bridge id)[UIImage imageNamed:@"circle.png"].CGImage;
    [self.view addSubview:self.outView];
        
    self.hourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(138, 100, 24, 100)];
    self.hourImageView.image = [UIImage imageNamed:@"hour.png"];
//
    self.minuteImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 90, 20, 120)];
    self.minuteImageView.image = [UIImage imageNamed:@"minute.png"];

    
    self.secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(142, 80, 16,140)];
    self.secondImageView.image = [UIImage imageNamed:@"second.png"];
    
    self.hourImageView.layer.anchorPoint = CGPointMake(0.5, 0.7);
    self.minuteImageView.layer.anchorPoint = CGPointMake(0.5, 0.7);
    self.secondImageView.layer.anchorPoint = CGPointMake(0.5, 0.7);
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"info ");
        [self tick];
    }];
    
    // 第一个参数:延迟的时间
    // 可以通过改变队列来改变线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 需要延迟执行的代码
        [self.outView addSubview:self.hourImageView];

        [self.outView addSubview:self.minuteImageView];

        [self.outView addSubview:self.secondImageView];

    });
    
}

-(void)tick {
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
    CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
    //calculate hour hand angle //calculate minute hand angle
    CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0;
    //calculate second hand angle
    CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0;
    //rotate hands

    NSLog(@"%f %f %f",hoursAngle,minsAngle,secsAngle);
    
    self.hourImageView.transform = CGAffineTransformMakeRotation(hoursAngle);
    self.minuteImageView.transform = CGAffineTransformMakeRotation(minsAngle);
    self.secondImageView.transform = CGAffineTransformMakeRotation(secsAngle);
    
}


-(void)chapter3 {
CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
        [self.view addSubview:customView];
}

-(void)chaptere2 {
    CALayer * blueLayer = [CALayer layer];
    blueLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    //set controller as layer delegate
    blueLayer.delegate = self;
    //ensure that layer backing image uses correct scale
    [self.view.layer addSublayer:blueLayer];
    //force layer to redraw
    [blueLayer display];

}

-(void)chaptere1 {
    self.outView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 267, 496)];
    self.outView.clipsToBounds = YES;
    [self.view addSubview:self.outView];
    
    CGFloat width = 267;
    CGFloat height = 496;
    
    self.coneView = [[UIView alloc] init];
    self.coneView.frame = CGRectMake(0, 0, width / 2, height / 2);
    self.coneView.backgroundColor = [UIColor redColor];
    [self.outView addSubview:self.coneView];
    
    
    self.shipView = [[UIView alloc] init];
    self.shipView.frame = CGRectMake(width / 2, 0, width / 2, height / 2);
    self.shipView.backgroundColor = [UIColor blueColor];
    
    [self.outView addSubview:self.shipView];
    
    self.iglooView = [[UIView alloc] init];
    self.iglooView.frame = CGRectMake(0, height / 2 , width / 2, height / 2);
    self.iglooView.backgroundColor = [UIColor cyanColor];
    
    [self.outView addSubview:self.iglooView];
    
    self.anchorView = [[UIView alloc] init];
    self.anchorView.frame = CGRectMake(width / 2, height / 2, width / 2, height / 2);
    self.anchorView.backgroundColor = [UIColor yellowColor];
    
    [self.outView addSubview:self.anchorView];
    
    NSArray * arrlayer = @[self.coneView.layer,self.shipView.layer,self.iglooView.layer,self.anchorView.layer];
    
    NSArray * array = @[@"girl100.jpg",@"girl116.jpg",@"girl92.jpg",@"girl36.jpg"];
    CGRect rectlt = CGRectMake(0, 0, 1, 1);
    CGRect rectrt = CGRectMake(0, 0, 1, 1);
    CGRect rectlb = CGRectMake(0, 0, 1, 1);
    CGRect rectrb = CGRectMake(0, 0, 1, 1);
    NSMutableArray * arrRect = [NSMutableArray arrayWithCapacity:4];
    [arrRect addObject:[NSValue valueWithCGRect:rectlt]];
    [arrRect addObject:[NSValue valueWithCGRect:rectrt]];
    [arrRect addObject:[NSValue valueWithCGRect:rectlb]];
    [arrRect addObject:[NSValue valueWithCGRect:rectrb]];
    
    
    for (int i = 0 ; i < arrRect.count; i ++) {
        UIImage * image = [UIImage imageNamed:array[i]];
        NSValue * value = arrRect[i];
        NSLog(@"%@",value);
        CGRect redct = value.CGRectValue;
        _index = i;
        [self addSpriteImage:image withContentRect:redct toLayer:arrlayer[i]];
    }
    
}

- (void)addSpriteImage:(UIImage *)image withContentRect:(CGRect)rect toLayer:(CALayer *)layer {
    
    layer.contents = (__bridge id)image.CGImage;
    // scale contents to fit
    layer.contentsGravity = kCAGravityResizeAspectFill;
    // set contentsRect
    layer.contentsRect = rect;
}



@end
